<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAdminTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->unsignedBigInteger('role_id')->comment('角色ID');
        });
        Schema::create('role', function (Blueprint $table) {
            $table->id();
            $table->string('name')->comment('角色名称');
            $table->unsignedBigInteger('parent_id')->default(0)->comment('上级角色');
            $table->timestamps();
        });
        Schema::create('menus', function (Blueprint $table) {
            $table->id();
            $table->string('key')->nullable()->comment('键值');
            $table->string('name')->comment('菜单名称');
            $table->string('icon')->nullable()->comment('菜单ICON');
            $table->string('link')->nullable()->comment('菜单跳转');
            $table->integer('status')->default(1)->comment('是否显示：1-显示 0-不显示');
            $table->unsignedBigInteger('weight')->default(1)->comment('菜单优先级');
            $table->unsignedBigInteger('parent_id')->default(0)->comment('上级菜单');
            $table->timestamps();
        });
        Schema::create('permission', function (Blueprint $table) {
            $table->id();
            $table->string('code')->comment('权限标示');
            $table->string('name')->nullable()->comment('权限名称');
            $table->string('module')->nullable()->comment('权限模块');
            $table->timestamps();
        });
        Schema::create('role_menus', function (Blueprint $table) {
            $table->unsignedBigInteger('role_id');
            $table->unsignedBigInteger('menus_id');

            $table->primary(['menus_id', 'role_id']);
        });
        Schema::create('role_permission', function (Blueprint $table) {
            $table->unsignedBigInteger('role_id');
            $table->unsignedBigInteger('permission_id');

            $table->primary(['permission_id', 'role_id']);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('role_id');
        });
        Schema::dropIfExists('role_permission');
        Schema::dropIfExists('role_menus');
        Schema::dropIfExists('role');
        Schema::dropIfExists('menus');
        Schema::dropIfExists('permission');
    }
}
